using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using Husb.Util;

namespace Husb.WinForm.Controls
{
    //class DataGridViewEnumTypeColumn
    public class DataGridViewEnumTypeColumn : DataGridViewColumn
    {
        public DataGridViewEnumTypeColumn()
            : base(new DataGridViewEnumTypeCell())
        { }

        public Type EnumType
        {
            get
            {
                if (this.CellTemplate == null)
                {
                    throw new InvalidOperationException("DataGridViewColumn_CellTemplateRequired");
                }

                return ((DataGridViewEnumTypeCell)this.CellTemplate).EnumType;
            }
            set
            {
                if (this.EnumType != value)
                {
                    ((DataGridViewEnumTypeCell)this.CellTemplate).EnumType = value;
                }
                if (base.DataGridView != null)
                {
                    DataGridViewRowCollection rows = base.DataGridView.Rows;
                    int count = rows.Count;
                    for (int i = 0; i < count; i++)
                    {
                        DataGridViewEnumTypeCell cell = rows.SharedRow(i).Cells[base.Index] as DataGridViewEnumTypeCell;
                        if (cell != null)
                        {
                            cell.EnumType = value;
                        }
                    }
                    base.DataGridView.InvalidateColumn(base.Index);
                }
            }
        }
    }

    public class DataGridViewEnumTypeCell : DataGridViewTextBoxCell
    {
        private Type enumType;
        public Type EnumType
        {
            get
            {
                return enumType;
            }
            set { enumType = value; }
        }

        protected override object GetFormattedValue(object value, int rowIndex, ref DataGridViewCellStyle cellStyle, System.ComponentModel.TypeConverter valueTypeConverter, System.ComponentModel.TypeConverter formattedValueTypeConverter, DataGridViewDataErrorContexts context)
        {
            object o = base.GetFormattedValue(value, rowIndex, ref cellStyle, valueTypeConverter, formattedValueTypeConverter, context);

            DataGridViewEnumTypeColumn emunColumn = (DataGridViewEnumTypeColumn)OwningColumn;
            enumType = emunColumn.EnumType;
            if (enumType == null)
            {
                return "";
            }

            string tmp = o as String;
            if (tmp != null)
            {
                if (tmp.Length > 4 || tmp.Length == 0)
                {
                    return o;
                }
                else
                {
                    return EnumUtil.GetDisplayName(enumType, Int32.Parse(tmp));
                }
            }

            return EnumUtil.GetDisplayName(enumType, (Int32)o);
        }
    }
}
